package com.holyn.guofang.view.home;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.apkfuns.logutils.LogUtils;
import com.holyn.guofang.R;
import com.holyn.guofang.view.distinguish.CommonDistinguishActivity;
import com.holyn.guofang.view.distinguish.PictureConfirmActivity;
import com.holyn.guofang.view.distinguish.SeniorDistinguishActivity;
import com.holyn.guofang.viewcommon.crop.CropActivity;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.compress.Luban;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * Created by Holyn on 2017/6/13.
 */

public class HomeDistinguishFragment extends Fragment {
    Unbinder unbinder;
    @BindView(R.id.iv_logo)
    ImageView ivLogo;
    @BindView(R.id.ll_album)
    LinearLayout llAlbum;
    @BindView(R.id.ll_take_photo)
    LinearLayout llTakePhoto;

    private String imagePath = "";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_distinguish, container, false);

        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.ll_album, R.id.ll_take_photo})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_album:
//                CommonDistinguishActivity.start(getActivity(), CommonDistinguishActivity.INTENT_ACTION_VALUE_OPEN_ALBUM);
//                openAlbum();
                PictureConfirmActivity.startForResult(getActivity(), PictureConfirmActivity.REQUEST_CODE_OPEN_ALBUM);
                break;
            case R.id.ll_take_photo:
//                CommonDistinguishActivity.start(getActivity(), CommonDistinguishActivity.INTENT_ACTION_VALUE_TAKE_PHOTO);
//                new RxPermissions(getActivity())
//                        .request(Manifest.permission.WRITE_EXTERNAL_STORAGE
//                                , Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
//                        .subscribe(new Consumer<Boolean>() {
//                            @Override
//                            public void accept(@NonNull Boolean aBoolean) throws Exception {
//                                takePhoto();
//                            }
//                        });

                PictureConfirmActivity.startForResult(getActivity(), PictureConfirmActivity.REQUEST_CODE_TAKE_PHOTO);

                break;
        }
    }

    private void openAlbum() {
        PictureSelector.create(getActivity())
                .openGallery(PictureMimeType.ofImage())
                .isCamera(false)
                .selectionMode(PictureConfig.SINGLE)
//                .compressGrade(Luban.FIRST_GEAR)// luban压缩档次，默认3档 Luban.FIRST_GEAR、Luban.CUSTOM_GEAR
                .compress(true)// 是否压缩
//                .compressMode(PictureConfig.LUBAN_COMPRESS_MODE)//系统自带 or 鲁班压缩 PictureConfig.SYSTEM_COMPRESS_MODE or LUBAN_COMPRESS_MODE
//                        .glideOverride(160, 160)// glide 加载宽高，越小图片列表越流畅，但会影响列表图片浏览的清晰度
//                        .compressMaxKB(100)//压缩最大值kb compressGrade()为Luban.CUSTOM_GEAR有效
//                        .compressWH(100, 100) // 压缩宽高比 compressGrade()为Luban.CUSTOM_GEAR有效
                .forResult(PictureConfig.CHOOSE_REQUEST);

    }

    private void takePhoto() {
        PictureSelector.create(getActivity())
                .openCamera(PictureMimeType.ofImage())
                .selectionMode(PictureConfig.SINGLE)
                .imageFormat(".jpg")
//                .compressGrade(Luban.CUSTOM_GEAR)// luban压缩档次，默认3档 Luban.FIRST_GEAR、Luban.CUSTOM_GEAR
//                .compressMaxKB(800)
                .compress(true)// 是否压缩
//                .compressMode(PictureConfig.LUBAN_COMPRESS_MODE)//系统自带 or 鲁班压缩 PictureConfig.SYSTEM_COMPRESS_MODE or LUBAN_COMPRESS_MODE
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }

//    @Override
//    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (resultCode == Activity.RESULT_OK) {
//            switch (requestCode) {
//                case PictureConfig.CHOOSE_REQUEST:
//                    // 图片选择结果回调
//                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
//                    // 例如 LocalMedia 里面返回三种path
//                    // 1.media.getPath(); 为原图path
//                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
//                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
//                    // 如果裁剪并压缩了，已取压缩路径为准，因为是先裁剪后压缩的
//                    LogUtils.d(selectList.get(0));
//                    imagePath = selectList.get(0).getCompressPath();
//                    CommonDistinguishActivity.start(getActivity(), imagePath);
//                    break;
//            }
//        }
//    }
}
